<template>
  <div>
    <labor @addChange="addChange"></labor>
    <myTable
      :tableInfo="tableInfo"
      :list="list"
      @configChange="configChange"
      @edit="getDetail(2, $event)"
      @del="del"
    ></myTable>
    <my-form ref="form" @refresh="getList"></my-form>
  </div>
</template>

<script>
import myTable from "./table"
import labor from "./table/labor"
import myForm from "./form"
import { distribution, distributionConfig } from "@/api/delivery"
import { list, detail, destroy } from "@/api/delivery"
export default {
  components: { myTable, labor, myForm },
  data() {
    return {
      list: [],
      tableInfo: {
        default_distribution_fee: "",
        no_distribution_money: "",
      },
    }
  },
  mounted() {
    this.getList()
  },
  methods: {
    // 获取配送方案
    async getList() {
      try {
        const { data } = await list()
        this.list = data
      } catch (error) {
        this.$msg.errorMsg(error.msg)
      }
    },
    // 获取默认配送费
    async getDistribution() {
      try {
        const { data } = await distribution()
        this.tableInfo.default_distribution_fee = data.default_distribution_fee
        this.tableInfo.no_distribution_money = data.no_distribution_money
      } catch (error) {
        this.$msg.errorMsg(error.msg)
      }
    },
    // 获取配送方案详情
    async getDetail(type, { scheme_id }) {
      try {
        const { data } = await detail({ scheme_id })
        this.$refs.form.show(type, data)
      } catch (error) {
        this.$msg.errorMsg(error.msg)
      }
    },
    // 设置默认配送费
    async configChange(e) {
      try {
        const data = await distributionConfig({
          default_distribution_fee: e.default_distribution_fee,
          no_distribution_money: e.no_distribution_money,
        })
        this.$msg.successMsg(data.msg)
        this.getDistribution()
      } catch (ex) {
        console.log(ex.msg)
      }
    },
    // 添加/修改
    addChange(type = 1, e) {
      this.$refs.form.show(type, e)
    },
    // 删除
    del(scheme_id) {
      const that = this
      that.$msg.massageConfirm("是否删除当前项？", async function () {
        try {
          const data = await destroy({ scheme_id })
          that.$msg.successMsg(data.msg)
          that.getList()
        } catch (ex) {
          console.log(ex.msg)
        }
      })
    },
  },
}
</script>

<style lang="scss" scoped></style>
